//
//  MCStoreDeliveryItemView.swift
//  StoreManage
//
//  Created by MC on 2017/12/27.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCStoreDeliveryItemView: UIView {

    
    var infoArray = NSArray() {
        didSet {
            
            
            for view in self.subviews {
                view.removeFromSuperview()
            }
            
            initUI()
            
            var allWidth : CGFloat = 0
            
            
            for str in infoArray {
                let itemStr = str as? String ?? ""
                let itemLength = itemStr.MCString_getWidth(font: 12, height: 16)
                
                let showLabel = UILabel()
                showLabel.layer.cornerRadius = 3
                showLabel.layer.masksToBounds = true
                showLabel.layer.borderColor = UIColor.MCMain.cgColor
                showLabel.layer.borderWidth = 1
                showLabel.font = MCFont(12)
                showLabel.textAlignment = NSTextAlignment.center
                showLabel.textColor = UIColor.MCMain
                showLabel.text = itemStr
                self.addSubview(showLabel)
                showLabel.snp.remakeConstraints { (make) ->Void in
                    make.size.equalTo(CGSize.init(width: itemLength + 10, height: 16))
                    make.bottom.equalTo(-10)
                    make.left.equalTo(38 + allWidth)
                }
                
                allWidth += (itemLength + 10 + 10)
            }
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       backgroundColor = UIColor.white
    }
    
    
    func initUI() {
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.bottom.right.equalTo(0)
            make.height.equalTo(1)
        }
        
        self.addSubview(deliveryButton)
        deliveryButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(8)
            make.size.equalTo(CGSize.init(width: 70, height: 35))
        }
        
        self.addSubview(editImageView)
        editImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(deliveryButton.snp.right).offset(10)
            make.centerY.equalTo(deliveryButton)
            make.size.equalTo(CGSize.init(width: 14, height: 14))
        }
        
        self.addSubview(inifoLabel)
        inifoLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.bottom.equalTo(-10)
        }
        
        
        self.addSubview(deliverySwitch)
        deliverySwitch.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.centerY.equalTo(self)
        }
        
        self.addSubview(showSettingButton)
        showSettingButton.snp.remakeConstraints { (make) ->Void in
            make.left.top.bottom.equalTo(self)
            make.right.equalTo(deliverySwitch.snp.left).offset(-10)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var deliveryButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(15)
        button.setTitleColor(UIColor.MCWords, for: UIControlState.normal)
        return button
    }()
    
    lazy var editImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Mine_edit").MCImageWithTintColor(color: UIColor.MCGray_middle)
        return imageView
    }()
    
    lazy var inifoLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.MCGray
        label.font = MCFont(13)
        return label
    }()
    
    lazy var deliverySwitch: UISwitch = {
        let sw = UISwitch()
        sw.onTintColor = UIColor.MCAssist_C
        sw.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
        return sw
    }()
    
    lazy var showSettingButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.backgroundColor = UIColor.clear
        return button
    }()
}
